package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author Xutao Bai
 * @Date created in 2020/1/3
 * @description
 * @Version 1.0
 */
@RestController
@RequestMapping("role")
@Slf4j
public class RoleController {
    @Reference
    private RoleService roleService;

    /*
    分页查询
     */
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[角色-分页]data:{}", queryPageBean);
        //rpc调用service
        PageResult pageResult = roleService.findByPage(queryPageBean);
        return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);
    }

    /**
     * 新增
     *
     * @param role
     * @param permissions
     * @param menus
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody Role role, Integer[] permissions, Integer[] menus) {
        log.info("[角色-添加]data:{},permissions:{},menus:{}", role, permissions, menus);
        roleService.add(role, permissions, menus);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("findById")
    public Result findById(Integer id) {
        log.info("[角色-编辑回显]id:{}", id);
        Role role = roleService.findById(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, role);
    }

    /**
     * 根据角色id查询权限id集合
     *
     * @param id
     * @return
     */
    @GetMapping("findPermissionsByRoleId")
    public Result findPermissionsByRoleId(Integer id) {
        log.info("[角色-编辑权限id回显]id:{}", id);
        List<Integer> permissions = roleService.findPermissionsByRoleId(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, permissions);
    }

    /**
     * 根据角色id查询菜单id集合
     *
     * @param id
     * @return
     */
    @GetMapping("findMenusByRoleId")
    public Result findMenusByRoleId(Integer id) {
        log.info("[角色-编辑菜单id回显]id:{}", id);
        List<Integer> menus = roleService.findMenusByRoleId(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, menus);
    }

    /**
     * 编辑
     *
     * @param role
     * @param permissions
     * @param menus
     * @return
     */
    @PostMapping("edit")
    public Result edit(@RequestBody Role role, Integer[] permissions, Integer[] menus) {
        log.info("[角色-编辑]data:{},permissions:{},menus:{}", role, permissions, menus);
        roleService.edit(role, permissions, menus);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("delete")
    public Result delete(Integer id) {
        log.info("[角色-删除]id:{}", id);
        roleService.delete(id);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    @GetMapping("/findAll")
    public Result findAll(){
        List<Role> roles =roleService.findAll();

        return new Result(true, MessageConst.ACTION_SUCCESS,roles);

    }
}
